var do_App = sm("do_App");
var do_Page = sm("do_Page");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var deviceone = require("deviceone");
var userUrl = require("url/user");
var qqHttp = require("helper/http");
var listdata = mm("do_ListData");
var helperOpen = require("helper/open");
var qqmath = require("util/qqmath");
//定义一个DateTimePicker对象
var do_DateTimePicker = sm("do_DateTimePicker")
// 本页面-ajax业务请求url
var httpUrl = userUrl.url.moneyLog.page;
var noData = [{"template":1}];

// UI
var headerAout = ui("headerAout");
var aLayoutRoot = ui("aLayoutRoot");
var listView = ui("listView");
var rootview = ui("$");
// 定义变量
var page = 1;
var labelStartTime=ui("labelStartTime");
var aLayoutStartTime=ui("aLayoutStartTime");
var labelEndTime=ui("labelEndTime");
var aLayoutEndTime=ui("aLayoutEndTime");
var do_Button_search=ui("do_Button_search");
var do_Label_withdrawCash=ui("do_Label_withdrawCash");
var do_Button_to_withdraw_cash=ui("do_Button_to_withdraw_cash");

//定义时间
var myDate=new Date(2015,01,01);
var date0 = myDate.getTime();

var myDate=new Date(2050,01,01);
var dateMax = myDate.getTime();

var searchBTime,searchETime;
//选择日期方式，最大日期为2050年最小日期为2001年，预设日期为当前时间，设置按钮文字
aLayoutStartTime.on("touch",function(){
	do_DateTimePicker.show({type:1, data:date0, minDate:date0,maxDate:dateMax, title:"日期选择", buttons:["取消","确定"]}, function(data, e){
        if(data.flag==1){
        	searchBTime=parseInt(data.time,10);
        	labelStartTime.text=timeFormat(searchBTime);
        }
    });
})
//选择日期方式，最大日期为2050年最小日期为2001年，预设日期为当前时间，设置按钮文字
aLayoutEndTime.on("touch",function(){
	do_DateTimePicker.show({type:1, data:date0,minDate:date0,maxDate:dateMax, title:"日期选择", buttons:["取消","确定"]}, function(data, e){
        if(data.flag==1){
        	searchETime=parseInt(data.time,10);
        	labelEndTime.text=timeFormat(searchETime);
        }
    });
})

function timeFormat(time){
	var svDate = new Date(time);//要格式化的时间
	return svDate.getFullYear()+"/"+(svDate.getMonth()+1)+"/"+svDate.getDate();
}
// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});

/* 头部 */
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});

//右上角-
var headerRight = ui(addheader + ".headerRight");
// 右上角添加+图标
var imgNext = ui(addheader + ".imgNext");
imgNext.source = "source://image/common/more.png";
imgNext.visible = true;

//头部标题
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"labelTitle" : "交易记录",
})
header.refreshData();

var menus = [ {
	"template" : 0,
	"menuIcon" : "source://image/menu/activity.png",
	"menuName" : "我的提现",
	"type" : "addCash"
}];

/*
 * 菜单弹出
 */
var popmenu = rootview.add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	popmenuf.fire("popmenuClose");// 关闭菜单
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	if (actionType == "addCash") {// 我的提现
		helperOpen.open("source://view/user/money/withdrawList.ui");
	} 
});

var type=2;
// 页面装载完成后，开始初始化工作
do_Page.on("loaded", function() {
	//读取当前页面的传入参数
	loadPage();
});
listView.bindItems(listdata);

do_Button_search.on("touch",function(data,e){
	loadPage();
})
do_Button_to_withdraw_cash.on("touch",function(data,e){
	if(parseFloat(do_Label_withdrawCash.text)<100){
		nf.toast("提现金额不足100，不允许提现。");
		return;
	}
	var wCash = qqmath.getDight(do_Label_withdrawCash.text,2)+'';
	helperOpen.open("source://view/user/money/withdraw.ui",{withdrawCash:wCash});
})
do_Page.on("result", function(data) {
	if(data&&data!=null&&"withdrawCash"==data.fromPage){
		do_Label_withdrawCash.text=qqmath.getDight(data.withdrawCash,2)+'';
	}
})
// 刷新数据
function loadPage() {
	page = 1;
	var params = {
		page : page,
		type:type
	};
	if(searchBTime){
		params.searchBTime=searchBTime;
	}
	if(searchETime){
		params.searchETime=searchETime;
	}
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.removeAll();
		if (data.page.content.length>0) {
			listdata.addData(data.page.content);
		}else{
			listdata.addData(noData);
		}
		do_Label_withdrawCash.text=qqmath.getDight(data.withdrawCash,2)+'';
		listView.refreshItems();
	})
}
// 刷新数据
function getNextPageData() {
	page++;
	var params = {
		page : page,
		type:type
	};
	if(searchBTime){
		params.searchBTime=searchBTime;
	}
	if(searchETime){
		params.searchETime=searchETime;
	}
	qqHttp.post(httpUrl, params, function(data) {
		listView.rebound();
		listdata.addData(data.page.content);
		listView.refreshItems();
	})
}

// 下拉列表，刷新数据
listView.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
	}
});

// 上拉列表，翻页数据
listView.on("push", function(data) {
	if (data.state == 2) {
		getNextPageData();
	}
});
